



//模型数据
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
///一般用于父组件对子组件的跨组件传值。
class InheritedTestModel {
  final int count;
  const InheritedTestModel(this.count);
}

//哨所（自定义InheritedWidget类）
class  InheritedContext extends InheritedWidget {

  //变量
  final InheritedTestModel inheritedTestModel;
  final Function() increment;
  final Function() reduce;

  InheritedContext({Key? key,
    required this.inheritedTestModel,
    required this.increment,
    required this.reduce,
    required Widget child,
  }) : super(key:key,child: child);

  //定义一个便捷方法，方便子树中的widget获取共享数据
  static InheritedContext? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<InheritedContext>();
  }

  //是否重建取决于Widget组件是否相同
  @override
  bool updateShouldNotify(InheritedContext oldWidget) {
    return  inheritedTestModel != oldWidget.inheritedTestModel;
  }

}

class TestWidgetA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("TestWidgetA build");
    var of = InheritedContext.of(context);
    return new Padding(
        padding: const EdgeInsets.only(left: 10,top: 10,right: 10),
        child: new RaisedButton(
          textColor: Colors.black,
            child: Text("+"),
            onPressed: of?.increment
        ),
    );
  }
}

class TestWidgetB extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("TestWidgetB build");
    var of = InheritedContext.of(context);
    return new Padding(
      padding: const EdgeInsets.only(left: 10,top: 10,right: 10),
      child: new RaisedButton(
          textColor: Colors.black,
          child: Text("-"),
          onPressed: of?.reduce
      ),
    );
  }
}

class TestWidgetC extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("TestWidgetC build");
    var of = InheritedContext.of(context);
    var model = of?.inheritedTestModel;
    return new Padding(
      padding: const EdgeInsets.only(left: 10,top: 10,right: 10),
      child: new RaisedButton(
          textColor: Colors.black,
          child: Text('${model?.count}'),
          onPressed: (){

          }
      ),
    );
  }
}

class InheritedWidgetTestContainer extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new InheritedWidgetTestContainerState();
  }
}
class InheritedWidgetTestContainerState extends State<InheritedWidgetTestContainer> {

  InheritedTestModel? _inheritedTestModel;

  _initData(){
    _inheritedTestModel = new InheritedTestModel(0);
  }

  @override
  void initState() {
    _initData();
    super.initState();
  }

  _incrementCount(){
    setState(() {
      _inheritedTestModel = new InheritedTestModel(1 + (_inheritedTestModel?.count??0));
    });
  }

  _reduceCount(){
    setState(() {
      _inheritedTestModel = new InheritedTestModel((_inheritedTestModel?.count??0) - 1);
    });
  }

  @override
  Widget build(BuildContext context) {
    return InheritedContext(
        inheritedTestModel: _inheritedTestModel!,
        increment: _incrementCount,
        reduce: _reduceCount,
        child: Scaffold(
          appBar: AppBar(
            title: Text('inheritedWidgetTest'),
          ),
          body: new Center(
            child: Column(
              children: [
                TestWidgetA(),
                TestWidgetB(),
                TestWidgetC()
              ],
            ),
          ),
        ));
  }

}